﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;

public partial class Validate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!User.Identity.IsAuthenticated)
        {
            Response.Redirect("./Error.aspx");
        }
        if(Request.QueryString["From"] != null)
        {
            this.Label1.Visible = true;
         }
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        Label label = Master.FindControl("Label1") as Label;
        if (label != null)
        {
            label.Text = "";
        }
        global::User user = UserManager.GetInstance().FindUser(User.Identity.Name);

        if(string.Equals(user.ValidateCode, this.TextBox1.Text.Trim(), StringComparison.OrdinalIgnoreCase))
        {
            user.ServiceFileName = user.UserName;
            foreach (char c in Path.GetInvalidFileNameChars())
            {
                user.ServiceFileName = user.ServiceFileName.Replace(c, '_');
            }
            user.ServiceFileName += ".xml";

            user.IsValidate = true;
            user.Money = 10;  // 初始币

            string fromUser = Session[ConstStrings.RefUser] as string;
            if (fromUser != null)
            {
                global::User refUser = UserManager.GetInstance().FindUser(fromUser);
                if (refUser != null)
                {
                    refUser.Money += 10;  // 推荐人奖励
                }
            }

            UserManager.GetInstance().Save();


            if (Session["LastUrl"] != null)
            {
                Response.Redirect(Session["LastUrl"].ToString(), true);
            }
            else
            {
                Response.Redirect("./Default.aspx");
            }
        }
        else
        {
            label.ForeColor =  Color.Red;
            label.Text = "验证码输入错误。";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        global::User user = UserManager.GetInstance().FindUser(User.Identity.Name);
        Utility.SendValidateCode(user.UserName, user.PhoneNumber, user.ValidateCode);
    }
}